package com.scorpio.baselibrary.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.StateSet;

import androidx.appcompat.widget.AppCompatImageView;

import com.scorpio.baselibrary.R;

/**
 * 有选中状态的 ImageView
 * created by 包子 on 2019/10/9
 */
public class CheckBoxImageView extends AppCompatImageView {
    private boolean checked = false;

    public CheckBoxImageView(Context context) {
        this(context, null);
    }

    public CheckBoxImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CheckBoxImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CheckBoxImageView);
        checked = array.getBoolean(R.styleable.CheckBoxImageView_checked, checked);
        setChecked(checked);
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
        setImageState(this.checked ? new int[]{android.R.attr.state_checked} : StateSet.NOTHING, true);
    }

    public boolean isChecked() {
        return checked;
    }
}
